﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using Avalonia.Animation;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Material.Icons;
using Material.Icons.Avalonia;
using ui.test.Views;

namespace ui_test.ViewModels;

public partial class MainWindowViewModel : ViewModelBase
{
    public string Greeting => "Welcome to Avalonia!";

    // [ObservableProperty] //这个注解observableProperty与以下的ShowWidth方法的功能是一样的，这样写起来是更简单
    // private int _defaultShowWidth = 300;

    // public int ShowWidth
    // {
    //     get => _defaultShowWidth;
    //     set => SetProperty(ref _defaultShowWidth, value);
    // }

    // public bool IsPaneOpen
    // {
    //     get => _isPaneOpen;
    //     set => SetProperty(ref _isPaneOpen, value);
    // }

    // public void ShowSide()
    // {
    //     if (DefaultShowWidth == 300)
    //     {
    //         DefaultShowWidth = 50;
    //     }
    //     else
    //     {
    //         DefaultShowWidth = 300;
    //     }
    // }

    [ObservableProperty] private bool _isPaneOpen = true;
    [ObservableProperty] private ItemDetail? _selectedItem;
    [ObservableProperty] private UserControl _currentPage = new FirstScreen();

    public ObservableCollection<string> DataSource { get; } = ["1", "2", "3"];

    public ObservableCollection<ItemDetail> ItemDetailSource { get; } =
    [
        new()
        {
            Name = "Page1",
            Path = @"C:\Users\tower\RiderProjects\ui-test\ui-test\Assets\avalonia-logo.ico",
            Control = new UserControl1()
        },

        new()
        {
            Name = "Page2",
            Path = @"C:\Users\tower\RiderProjects\ui-test\ui-test\Assets\avalonia-logo.ico",
            Control = new UserControl2()
        },
        new()
        {
            Name = "Page3",
            Path = "",
            Control = new UserControl3()
        }
    ];

    public ObservableCollection<StackPanel> NewDataSource { get; } =
    [
        new()
        {
            Children =
            {
                new TextBlock()
                {
                    Text = "Page1"
                },
                new UserControl()
                {
                    Content = new MaterialIconExt(MaterialIconKind.Kitchen)
                }
            }
        },

        new()
        {
            Children =
            {
                new TextBlock()
                {
                    Text = "Page2"
                },
                new UserControl()
                {
                    Content = new MaterialIconExt(MaterialIconKind.Keep)
                }
            }
        }
    ];

    private static Dictionary<string, ItemDetail> pageMap = new();

    partial void OnSelectedItemChanged(ItemDetail? value)
    {
        Console.WriteLine(value);
        if (value == null)
        {
            return;
        }

        // var transition = new CrossFade(TimeSpan.FromSeconds(3));
        var newPage = pageMap[value.Name];
        var transition = new PageSlide(TimeSpan.FromMilliseconds(300));
        transition.Start(CurrentPage, newPage.Control, forward: true, CancellationToken.None);
        CurrentPage = newPage.Control;
    }


    [RelayCommand]
    private void TriggerPanel()
    {
        IsPaneOpen = !IsPaneOpen;
    }


    public MainWindowViewModel()
    {
        // DataSource.Clear();
        // foreach (var pageMapKey in pageMap.Keys)
        // {
        //     DataSource.Add(pageMapKey);
        // }
        foreach (var itemDetail in ItemDetailSource)
        {
            pageMap[itemDetail.Name] = itemDetail;
        }
    }

    public class ItemDetail
    {
        public string Name { get; set; }
        public string Path { get; set; }
        public UserControl Control { get; set; }
    }
}